home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / cmathmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  7.7 KB  |  431 lines

  1. /* Complex math module */
  2.  
  3. /* much code borrowed from mathmodule.c */
  4.  
  5. #include "Python.h"
  6.  
  7. #include "mymath.h"
  8. #include "protos/cmathmodule.h"
  9.  
  10. #ifdef i860
  11. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  12. #undef HUGE_VAL
  13. #endif
  14.  
  15. #ifdef HUGE_VAL
  16. #define CHECK(x) if (errno != 0) ; \
  17.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  18.     else errno = ERANGE
  19. #else
  20. #define CHECK(x) /* Don't know how to check */
  21. #endif
  22.  
  23. #ifndef M_PI
  24. #define M_PI (3.141592653589793239)
  25. #endif
  26.  
  27. /* First, the C functions that do the real work */
  28.  
  29. /* constants */
  30. static Py_complex c_1 = {1., 0.};
  31. static Py_complex c_half = {0.5, 0.};
  32. static Py_complex c_i = {0., 1.};
  33. static Py_complex c_i2 = {0., 0.5};
  34. #if 0
  35. static Py_complex c_mi = {0., -1.};
  36. static Py_complex c_pi2 = {M_PI/2., 0.};
  37. #endif
  38.  
  39. /* forward declarations */
  40. staticforward Py_complex c_log();
  41. staticforward Py_complex c_prodi();
  42. staticforward Py_complex c_sqrt();
  43.  
  44.  
  45. static Py_complex c_acos(x)
  46.     Py_complex x;
  47. {
  48.     return c_neg(c_prodi(c_log(c_sum(x,c_prod(c_i,
  49.             c_sqrt(c_diff(c_1,c_prod(x,x))))))));
  50. }
  51.  
  52. static char c_acos_doc [] =
  53. "acos(x)\n\
  54. \n\
  55. Return the arc cosine of x.";
  56.  
  57.  
  58. static Py_complex c_acosh(x)
  59.     Py_complex x;
  60. {
  61.     return c_log(c_sum(x,c_prod(c_i,
  62.             c_sqrt(c_diff(c_1,c_prod(x,x))))));
  63. }
  64.  
  65. static char c_acosh_doc [] =
  66. "acosh(x)\n\
  67. \n\
  68. Return the hyperbolic arccosine of x.";
  69.  
  70.  
  71. static Py_complex c_asin(x)
  72.     Py_complex x;
  73. {
  74.     return c_neg(c_prodi(c_log(c_sum(c_prod(c_i,x),
  75.             c_sqrt(c_diff(c_1,c_prod(x,x)))))));
  76. }
  77.  
  78. static char c_asin_doc [] =
  79. "asin(x)\n\
  80. \n\
  81. Return the arc sine of x.";
  82.  
  83.  
  84. static Py_complex c_asinh(x)
  85.     Py_complex x;
  86. {
  87.     /* Break up long expression for WATCOM */
  88.     Py_complex z;
  89.     z = c_sum(c_1,c_prod(x,x));
  90.     z = c_diff(c_sqrt(z),x);
  91.     return c_neg(c_log(z));
  92. }
  93.  
  94. static char c_asinh_doc [] =
  95. "asinh(x)\n\
  96. \n\
  97. Return the hyperbolic arc sine of x.";
  98.  
  99.  
  100. static Py_complex c_atan(x)
  101.     Py_complex x;
  102. {
  103.     return c_prod(c_i2,c_log(c_quot(c_sum(c_i,x),c_diff(c_i,x))));
  104. }
  105.  
  106. static char c_atan_doc [] =
  107. "atan(x)\n\
  108. \n\
  109. Return the arc tangent of x.";
  110.  
  111.  
  112. static Py_complex c_atanh(x)
  113.     Py_complex x;
  114. {
  115.     return c_prod(c_half,c_log(c_quot(c_sum(c_1,x),c_diff(c_1,x))));
  116. }
  117.  
  118. static char c_atanh_doc [] =
  119. "atanh(x)\n\
  120. \n\
  121. Return the hyperbolic arc tangent of x.";
  122.  
  123.  
  124. static Py_complex c_cos(x)
  125.     Py_complex x;
  126. {
  127.     Py_complex r;
  128.     r.real = cos(x.real)*cosh(x.imag);
  129.     r.imag = -sin(x.real)*sinh(x.imag);
  130.     return r;
  131. }
  132.  
  133. static char c_cos_doc [] =
  134. "cos(x)\n\
  135. \n\
  136. Return the cosine of x.";
  137.  
  138.  
  139. static Py_complex c_cosh(x)
  140.     Py_complex x;
  141. {
  142.     Py_complex r;
  143.     r.real = cos(x.imag)*cosh(x.real);
  144.     r.imag = sin(x.imag)*sinh(x.real);
  145.     return r;
  146. }
  147.  
  148. static char c_cosh_doc [] =
  149. "cosh(x)\n\
  150. \n\
  151. Return the hyperbolic cosine of x.";
  152.  
  153.  
  154. static Py_complex c_exp(x)
  155.     Py_complex x;
  156. {
  157.     Py_complex r;
  158.     double l = exp(x.real);
  159.     r.real = l*cos(x.imag);
  160.     r.imag = l*sin(x.imag);
  161.     return r;
  162. }
  163.  
  164. static char c_exp_doc [] =
  165. "exp(x)\n\
  166. \n\
  167. Return the exponential value e**x.";
  168.  
  169.  
  170. static Py_complex c_log(x)
  171.     Py_complex x;
  172. {
  173.     Py_complex r;
  174.     double l = hypot(x.real,x.imag);
  175.     r.imag = atan2(x.imag, x.real);
  176.     r.real = log(l);
  177.     return r;
  178. }
  179.  
  180. static char c_log_doc [] =
  181. "log(x)\n\
  182. \n\
  183. Return the natural logarithm of x.";
  184.  
  185.  
  186. static Py_complex c_log10(x)
  187.     Py_complex x;
  188. {
  189.     Py_complex r;
  190.     double l = hypot(x.real,x.imag);
  191.     r.imag = atan2(x.imag, x.real)/log(10.);
  192.     r.real = log10(l);
  193.     return r;
  194. }
  195.  
  196. static char c_log10_doc [] =
  197. "log10(x)\n\
  198. \n\
  199. Return the base-10 logarithm of x.";
  200.  
  201.  
  202. /* internal function not available from Python */
  203. static Py_complex c_prodi(x)
  204.      Py_complex x;
  205. {
  206.     Py_complex r;
  207.     r.real = -x.imag;
  208.     r.imag = x.real;
  209.     return r;
  210. }
  211.  
  212.  
  213. static Py_complex c_sin(x)
  214.     Py_complex x;
  215. {
  216.     Py_complex r;
  217.     r.real = sin(x.real)*cosh(x.imag);
  218.     r.imag = cos(x.real)*sinh(x.imag);
  219.     return r;
  220. }
  221.  
  222. static char c_sin_doc [] =
  223. "sin(x)\n\
  224. \n\
  225. Return the sine of x.";
  226.  
  227.  
  228. static Py_complex c_sinh(x)
  229.     Py_complex x;
  230. {
  231.     Py_complex r;
  232.     r.real = cos(x.imag)*sinh(x.real);
  233.     r.imag = sin(x.imag)*cosh(x.real);
  234.     return r;
  235. }
  236.  
  237. static char c_sinh_doc [] =
  238. "sinh(x)\n\
  239. \n\
  240. Return the hyperbolic sine of x.";
  241.  
  242.  
  243. static Py_complex c_sqrt(x)
  244.     Py_complex x;
  245. {
  246.     Py_complex r;
  247.     double s,d;
  248.     if (x.real == 0. && x.imag == 0.)
  249.         r = x;
  250.     else {
  251.         s = sqrt(0.5*(fabs(x.real) + hypot(x.real,x.imag)));
  252.         d = 0.5*x.imag/s;
  253.         if (x.real > 0.) {
  254.             r.real = s;
  255.             r.imag = d;
  256.         }
  257.         else if (x.imag >= 0.) {
  258.             r.real = d;
  259.             r.imag = s;
  260.         }
  261.         else {
  262.             r.real = -d;
  263.             r.imag = -s;
  264.         }
  265.     }
  266.     return r;
  267. }
  268.  
  269. static char c_sqrt_doc [] =
  270. "sqrt(x)\n\
  271. \n\
  272. Return the square root of x.";
  273.  
  274.  
  275. static Py_complex c_tan(x)
  276.     Py_complex x;
  277. {
  278.     Py_complex r;
  279.     double sr,cr,shi,chi;
  280.     double rs,is,rc,ic;
  281.     double d;
  282.     sr = sin(x.real);
  283.     cr = cos(x.real);
  284.     shi = sinh(x.imag);
  285.     chi = cosh(x.imag);
  286.     rs = sr*chi;
  287.     is = cr*shi;
  288.     rc = cr*chi;
  289.     ic = -sr*shi;
  290.     d = rc*rc + ic*ic;
  291.     r.real = (rs*rc+is*ic)/d;
  292.     r.imag = (is*rc-rs*ic)/d;
  293.     return r;
  294. }
  295.  
  296. static char c_tan_doc [] =
  297. "tan(x)\n\
  298. \n\
  299. Return the tangent of x.";
  300.  
  301.  
  302. static Py_complex c_tanh(x)
  303.     Py_complex x;
  304. {
  305.     Py_complex r;
  306.     double si,ci,shr,chr;
  307.     double rs,is,rc,ic;
  308.     double d;
  309.     si = sin(x.imag);
  310.     ci = cos(x.imag);
  311.     shr = sinh(x.real);
  312.     chr = cosh(x.real);
  313.     rs = ci*shr;
  314.     is = si*chr;
  315.     rc = ci*chr;
  316.     ic = si*shr;
  317.     d = rc*rc + ic*ic;
  318.     r.real = (rs*rc+is*ic)/d;
  319.     r.imag = (is*rc-rs*ic)/d;
  320.     return r;
  321. }
  322.  
  323. static char c_tanh_doc [] =
  324. "tanh(x)\n\
  325. \n\
  326. Return the hyperbolic tangent of x.";
  327.  
  328.  
  329. /* And now the glue to make them available from Python: */
  330.  
  331. static PyObject *
  332. math_error()
  333. {
  334.     if (errno == EDOM)
  335.         PyErr_SetString(PyExc_ValueError, "math domain error");
  336.     else if (errno == ERANGE)
  337.         PyErr_SetString(PyExc_OverflowError, "math range error");
  338.     else    /* Unexpected math error */
  339.         PyErr_SetFromErrno(PyExc_ValueError); 
  340.     return NULL;
  341. }
  342.  
  343. static PyObject *
  344. math_1(args, func)
  345.     PyObject *args;
  346.     Py_complex (*func) Py_FPROTO((Py_complex));
  347. {
  348.     Py_complex x;
  349.     if (!PyArg_ParseTuple(args, "D", &x))
  350.         return NULL;
  351.     errno = 0;
  352.     PyFPE_START_PROTECT("complex function", return 0)
  353.     x = (*func)(x);
  354.     PyFPE_END_PROTECT(x)
  355.     CHECK(x.real);
  356.     CHECK(x.imag);
  357.     if (errno != 0)
  358.         return math_error();
  359.     else
  360.         return PyComplex_FromCComplex(x);
  361. }
  362.  
  363. #ifdef HAVE_PROTOTYPES
  364. #define FUNC1(stubname, func) \
  365.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  366.         return math_1(args, func); \
  367.     }
  368. #else /* !HAVE_PROTOTYPES */
  369. #define FUNC1(stubname, func) \
  370.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  371.         return math_1(args, func); \
  372.     }
  373. #endif
  374.  
  375. FUNC1(cmath_acos, c_acos)
  376. FUNC1(cmath_acosh, c_acosh)
  377. FUNC1(cmath_asin, c_asin)
  378. FUNC1(cmath_asinh, c_asinh)
  379. FUNC1(cmath_atan, c_atan)
  380. FUNC1(cmath_atanh, c_atanh)
  381. FUNC1(cmath_cos, c_cos)
  382. FUNC1(cmath_cosh, c_cosh)
  383. FUNC1(cmath_exp, c_exp)
  384. FUNC1(cmath_log, c_log)
  385. FUNC1(cmath_log10, c_log10)
  386. FUNC1(cmath_sin, c_sin)
  387. FUNC1(cmath_sinh, c_sinh)
  388. FUNC1(cmath_sqrt, c_sqrt)
  389. FUNC1(cmath_tan, c_tan)
  390. FUNC1(cmath_tanh, c_tanh)
  391.  
  392.  
  393. static char module_doc [] =
  394. "This module is always available. It provides access to mathematical\n\
  395. functions for complex numbers.";
  396.  
  397.  
  398. static PyMethodDef cmath_methods[] = {
  399.     {"acos", cmath_acos, 1, c_acos_doc},
  400.     {"acosh", cmath_acosh, 1, c_acosh_doc},
  401.     {"asin", cmath_asin, 1, c_asin_doc},
  402.     {"asinh", cmath_asinh, 1, c_asinh_doc},
  403.     {"atan", cmath_atan, 1, c_atan_doc},
  404.     {"atanh", cmath_atanh, 1, c_atanh_doc},
  405.     {"cos", cmath_cos, 1, c_cos_doc},
  406.     {"cosh", cmath_cosh, 1, c_cosh_doc},
  407.     {"exp", cmath_exp, 1, c_exp_doc},
  408.     {"log", cmath_log, 1, c_log_doc},
  409.     {"log10", cmath_log10, 1, c_log10_doc},
  410.     {"sin", cmath_sin, 1, c_sin_doc},
  411.     {"sinh", cmath_sinh, 1, c_sinh_doc},
  412.     {"sqrt", cmath_sqrt, 1, c_sqrt_doc},
  413.     {"tan", cmath_tan, 1, c_tan_doc},
  414.     {"tanh", cmath_tanh, 1, c_tanh_doc},
  415.     {NULL,        NULL}        /* sentinel */
  416. };
  417.  
  418. DL_EXPORT(void)
  419. initcmath()
  420. {
  421.     PyObject *m, *d, *v;
  422.     
  423.     m = Py_InitModule3("cmath", cmath_methods, module_doc);
  424.     d = PyModule_GetDict(m);
  425.     PyDict_SetItemString(d, "pi",
  426.                  v = PyFloat_FromDouble(atan(1.0) * 4.0));
  427.     Py_DECREF(v);
  428.     PyDict_SetItemString(d, "e", v = PyFloat_FromDouble(exp(1.0)));
  429.     Py_DECREF(v);
  430. }
  431.